package com.junsom;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by hejunsong on 2016-9-2.
 */
public class FileNio {

    public static void lines() throws FileNotFoundException, UnsupportedEncodingException {
        File file = new File("C:\\Users\\hejunsong\\IdeaProjects\\java8-demo\\src\\main\\resources\\琵琶行.txt");
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
        BufferedReader reader = new BufferedReader(isr);

        reader.lines().forEach(System.out::println);
        System.out.println("BufferedReader.lines 结束");
    }

    public static void fileMethod() throws IOException {
        File directory = new File("");// 参数为空
        String courseFile = directory.getCanonicalPath();
        System.out.println("项目根路径: " + courseFile);

        Files.lines(Paths.get(courseFile + "\\src\\main\\resources\\琵琶行.txt"), StandardCharsets.UTF_8).forEach(System.out::println);
        System.out.println("Files.lines 结束");

        Files.walk(Paths.get(courseFile), 2).forEach(System.out::println);
        System.out.println("Files.walk 结束");

        Files.find(Paths.get(courseFile), 100, (path, attr) -> {
            //System.out.println("" + path.getFileName());
            return String.valueOf(path).endsWith(".txt");
        }).forEach(System.out::println);
        System.out.println("Files.find 结束");

    }

    public static void main(String[] args) throws IOException {
        FileNio.lines();
        FileNio.fileMethod();
    }
}
